﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：12/20/2024 11:25:52 AM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion

namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:TreeDataGridView
    /// 描    述:
    /// 修改时间:12/20/2024 11:25:52 AM
    /// </summary>
    public class TreeDataGridView:DataGridView
    {
        #region 成员变量

        #region private



        #endregion


        #region protected
        protected TreeDataGridViewTextBoxColumn m_columnTreeHead;


        #endregion


        #region public
        /// <summary>
        /// 是否允许多选
        /// </summary>
        public bool MultiCheck
        {
            get;
            set;
        }

        public event EventHandler<DataEventArgs<int>> RowCheckedChanged;

        /// <summary>
        /// 选中的树状行
        /// </summary>
        public TreeDataGridViewRow[] SelectedTreeRows
        {
            get
            {
                List<TreeDataGridViewRow> rlt = new List<TreeDataGridViewRow>();
                foreach (DataGridViewRow row in this.Rows)
                {
                    if (row as TreeDataGridViewRow != null)
                    {
                        TreeDataGridViewRow opsrow = row as TreeDataGridViewRow;
                        if (opsrow.Checked == CheckState.Checked)
                        {
                            rlt.Add(opsrow);
                        }

                    }
                }
                return rlt.ToArray();
            }
        }

        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:12/20/2024 11:25:52 AM
        /// </summary>
        public TreeDataGridView()
        {
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.RowsAdded += OnTreeDataGridViewRowsAdded;
            this.RowsRemoved += OnTreeDataGridViewRowsRemoved;
        }
        #endregion

        #region 父类函数重载、接口实现
        /// <summary>
        /// 初始化树状列 ps:无法先添加，不然再设计器中新建时总是会在生成一列
        /// </summary>
        public virtual void InitializeTreeColumn()
        {
            m_columnTreeHead = new TreeDataGridViewTextBoxColumn();
            this.Columns.Insert(0, m_columnTreeHead);
        }


        public virtual TreeDataGridViewRow CreateRow()
        {
            return new TreeDataGridViewRow();
        }

        public virtual TreeDataGridViewRow AddTreeRow()
        {
            TreeDataGridViewRow rlt = CreateRow();
            int index = this.Rows.Add(rlt);
            return (TreeDataGridViewRow)this.Rows[index];
        }

        protected virtual void OnTreeDataGridViewRowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (e.RowIndex >= this.RowCount)
            {
                return;
            }
            DataGridViewRow row = this.Rows[e.RowIndex];
            if (row as TreeDataGridViewRow != null)
            {
                //foreach(DataGridViewRow crow in ((TreeDataGridViewRow)row).Rows)
                //{
                //    this.Rows.Remove(crow);
                //}
                ((TreeDataGridViewRow)row).CheckStateChanged -= OnTreeRowCheckStateChanged;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
        }
        protected virtual void OnTreeDataGridViewRowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            DataGridViewRow row = this.Rows[e.RowIndex];
            if (row as TreeDataGridViewRow != null)
            {
                ((TreeDataGridViewRow)row).CheckStateChanged += OnTreeRowCheckStateChanged;
            }
        }


        protected virtual void OnCurrentRowCheckedChanged(TreeDataGridViewRow trow)
        {
            if (trow != null && trow.Checked == CheckState.Checked && !this.MultiCheck)
            {
                foreach (DataGridViewRow row in this.Rows)
                {
                    TreeDataGridViewRow current;
                    if ((current = row as TreeDataGridViewRow) != null &&
                        current.Checked != CheckState.Indeterminate)
                    {
                        current.CheckStateChanged -= OnTreeRowCheckStateChanged;
                        current.Checked = current == trow ? CheckState.Checked : CheckState.Unchecked;
                        current.CheckStateChanged += OnTreeRowCheckStateChanged;

                    }
                }
            }
        }


        /// <summary>
        /// 展开所有行
        /// </summary>
        public void ExpandAll()
        {
            foreach (DataGridViewRow row in this.Rows)
            {
                if (row as TreeDataGridViewRow != null)
                {
                    ((TreeDataGridViewRow)row).ExpandAll();
                }
            }
        }


        /// <summary>
        /// 折叠所有行
        /// </summary>
        public void CollapseAll()
        {
            foreach (DataGridViewRow row in this.Rows)
            {
                if (row as TreeDataGridViewRow != null)
                {
                    ((TreeDataGridViewRow)row).Collapse();
                }
            }
        }

        protected void OnTreeRowCheckStateChanged(object sender, EventArgs e)
        {
            TreeDataGridViewRow trow = sender as TreeDataGridViewRow;
            OnCurrentRowCheckedChanged(trow);
            RowCheckedChanged?.Invoke(this, new DataEventArgs<int>(trow.Index));
            this.Refresh();
        }
        #endregion

        #region 函数

        #region private



        #endregion


        #region protected



        #endregion


        #region public


        #endregion

        #endregion
    }
}
